<?php

class system_model extends database_mysql
{           
    public function module_tbl_list(){
        $arr = parent::mysql_tbl_list();
        parent::mysql_close_connection();
        return $arr;
    }
    public function model_get_lang($type)
    {
        $query = "SELECT * FROM ".prefix."language WHERE `Status` = 'true' AND `".$type."` = 1 ORDER BY ID ASC LIMIT 0, 1";
        $result = parent::mysql_fetch_one_rows($query);
        parent::mysql_close_connection();
        return $result;
    }
	
	public function model_get_tbl_act($_com,$_com_id,$seokey)
	{
		$seokey = parent::mysql_check_string($seokey);
		$arrTbl = parent::mysql_tbl_list();
		$feild = parent::mysql_get_column_type(prefix. $_com .'_lang','SeoKey');
		$result = NULL;
		if(in_array(prefix. $_com .'_lang',$arrTbl) && $feild){
			$query = "
					SELECT
						prefix_actions.ActName
					FROM
						".prefix.$_com."
						INNER JOIN ".prefix.$_com."_lang ON ".prefix.$_com."_lang.".ucfirst($_com)."ID = ".prefix.$_com.".ID
						INNER JOIN ".prefix."actions ON ".prefix.$_com.".ActID = ".prefix."actions.ID
					WHERE
						".prefix.$_com."_lang.SeoKey = '".$seokey."' AND
						".prefix."actions.`Status` = 'true'
			";
				
			$result = parent::mysql_fetch_one_rows($query); 
		}
		parent::mysql_close_connection();
		return $result;
	 }
    
        
    public function get_module($_app_id,$_com_id,$position)
	{
		$position = parent::mysql_check_string($position);
        $query = "
			SELECT
				". prefix ."modules.`Name`
			FROM
				". prefix ."modules_position,
				". prefix ."modules 
			WHERE
				". prefix ."modules_position.ModuleID = ". prefix ."modules.ID AND
				". prefix ."modules_position.ComID = '".$_com_id."' AND
                ". prefix ."modules.AppID = '".$_app_id."' AND
				". prefix ."modules_position.Position = '".$position."' AND 
				". prefix ."modules_position.`Status`='true' 
			ORDER BY ". prefix ."modules_position.`order` ASC
		";
        
		$result = parent::mysql_fetch_all_rows($query);	

		parent::mysql_close_connection();
		return $result;
	}
    
    
    public function model_get_action($seokey,$ComID=NULL)
	{
		$seokey = parent::mysql_check_string($seokey);
        $arrAction['ActName']='';
        
        $arrAction = parent::mysql_fetch_one_rows(" 
                    SELECT
                        ". prefix ."actions.ActName
                    FROM
						". prefix ."actions
						INNER JOIN ". prefix ."actions_lang 
                        ON ". prefix ."actions.ID = ". prefix ."actions_lang.ActID
					WHERE
						". prefix ."actions_lang.SeoKey = '".$seokey."' AND
						". prefix ."actions.`Status` = 'true'
        ");
        
		parent::mysql_close_connection();
        return $arrAction['ActName'];
    
    }
    
	public function model_get_router($seoKey)
	{
		$seoKey = parent::mysql_check_string($seoKey);
		
		$arrRouter = parent::mysql_fetch_one_rows("
                SELECT
                    ". prefix ."categories.App AS AppName,
                    ". prefix ."categories.Com  AS ComName,
                    ". prefix ."actions.ID AS ActID,
                    ". prefix ."actions.ActName
                FROM
                    ". prefix ."categories_lang 
                    join ". prefix ."categories ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID 
                    LEFT JOIN ". prefix ."actions ON ". prefix ."actions.ID = ". prefix ."categories.Act
                WHERE
                    ". prefix ."categories_lang.SeoKey = '".$seoKey."' AND
                    ". prefix ."categories_lang.LangID = '".LANG."' AND
                    ". prefix ."categories_lang.`Status` = 'show'
        ");
		parent::mysql_close_connection();
		return $arrRouter;
	}
    
    public function model_get_app($seo_current)
    {
		$seo_current = parent::mysql_check_string($seo_current);
		
        if($seo_current=='admin'){
            return 'admin';
        }
        if($seo_current==''){
            return 'public';
        }
        
        $row = parent::mysql_fetch_one_rows("
            SELECT
                ". prefix ."categories.App
            FROM
                ". prefix ."categories
                INNER JOIN ". prefix ."categories_lang ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
            WHERE
                ". prefix ."categories_lang.SeoKey = '".trim($seo_current)."' AND
                ". prefix ."categories_lang.LangID = ". LANG ." AND
                ". prefix ."categories_lang.`Status` = 'show'
        ");
        
        $app = $row ['App'];
        if( empty($app) )
        {
            $app = $this->model_get_app(''.SEO_DEFAULT.'');
        }
		parent::mysql_close_connection();
 		return $app;
    }
        
    public function model_get_app_seokey($dirname)
    {
		$dirname = parent::mysql_check_string($dirname);
		
        $row = parent::mysql_fetch_one_rows("
            SELECT 
                ". prefix ."categories_lang.SeoKey
            FROM
                ". prefix ."categories
                INNER JOIN ". prefix ."categories_lang ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
            WHERE
                ". prefix ."categories.App = '". trim($dirname) ."' AND
                ". prefix ."categories_lang.LangID = ". LANG ." AND
                ". prefix ."categories_lang.`Status` = 'show'
        ");
        $app = $row['SeoKey'];
		parent::mysql_close_connection();
 		return $app;
    }
	    
    public function model_get_seokey_act($action)
    {
		
		$action = parent::mysql_check_string($action);
		
        $rs = parent::mysql_fetch_one_rows("SELECT
                " . prefix . "actions_lang.SeoKey
                FROM
                " . prefix . "actions
                INNER JOIN " . prefix . "actions_lang ON " . prefix . "actions.ID = " . prefix . "actions_lang.ActID
                WHERE
                " . prefix . "actions_lang.LangID = " . LANG ." AND
                " . prefix . "actions.ActName = '" . $action . "'");
		parent::mysql_close_connection();		
        if(empty($rs)){
            return false;
        }else{
            return $rs;
        }
    }
	
	public function model_get_seokey_com($com)
    {
		$com = parent::mysql_check_string($com);
		$rs = parent::mysql_fetch_one_rows("
            SELECT 
                ". prefix ."categories_lang.SeoKey
            FROM
                ". prefix ."categories
                INNER JOIN ". prefix ."categories_lang ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
            WHERE
                ". prefix ."categories.App = 'admin' AND
                ". prefix ."categories_lang.LangID = ". LANG ." AND
                ". prefix ."categories.Com = '". trim($com) ."' AND
                ". prefix ."categories.Define ='com'
        ");
		parent::mysql_close_connection();		
        if(empty($rs)){
            return false;
        }else{
            return $rs;
        }
    }

    public function model_tool_set_status($Table,$Status,$where)
    {
        $done = parent::mysql_update_insert("UPDATE $Table SET Status='".$Status."'  WHERE ".$where);
        parent::mysql_close_connection();
        return $done;
    }

    public function model_tool_delete($Table,$where)
    {
        $done = parent::mysql_delete("DELETE FROM ". $Table ." WHERE ".$where);

        parent::mysql_close_connection();
        return $done;

    }

    public function model_get_app_id($app)
    {
        $app = parent::mysql_check_string($app);

        $row = parent::mysql_fetch_one_rows("
            SELECT
                ". prefix ."categories.ID
            FROM
                ". prefix ."categories
            WHERE
                ". prefix ."categories.App = '". trim($app) ."' AND
                ". prefix ."categories.Define ='app'
        ");
        $ID = $row['ID'];
        parent::mysql_close_connection();
        return $ID;
    }

    public function model_get_com_id($com)
    {
        $com = parent::mysql_check_string($com);
        $row = parent::mysql_fetch_one_rows("
            SELECT
                ". prefix ."categories.ID
            FROM
                ". prefix ."categories
            WHERE
                ". prefix ."categories.Com = '". trim($com) ."' AND
                ". prefix ."categories.Define ='com'
        ");
        $ID = $row['ID'];
        parent::mysql_close_connection();
        return $ID;
    }
	
	
	public function model_get_group_user($user_id)
	{
        $rs = parent::mysql_fetch_all_rows("
				SELECT
                " . prefix . "user_group.GroupID
                FROM
                " . prefix . "user_group
                WHERE
                " . prefix . "user_group.UserID = " . $user_id);
		parent::mysql_close_connection();
        if(empty($rs)){
            return false;
        }else{
            return $rs;
        }
    }
    
    public function model_get_permission($user_id,$com_id, $action, $group)
	{   
		$action = parent::mysql_check_string($action);
        $sql = "
            SELECT
            ".prefix."permission.Action
            FROM
            ".prefix."categories
            INNER JOIN ".prefix."permission ON ".prefix."categories.ID = ".prefix."permission.CID
            INNER JOIN ".prefix."group_permiss ON ".prefix."permission.ID = ".prefix."group_permiss.PremissID
            INNER JOIN ".prefix."groups ON ".prefix."group_permiss.GroupID = ".prefix."groups.ID
            INNER JOIN ".prefix."user_group ON ".prefix."user_group.GroupID = ".prefix."groups.ID
            WHERE
            ".prefix."categories.ID = $com_id AND
            ".prefix."permission.`Status` = 'true' AND
			".prefix."permission.Action = '". $action ."' AND
            ".prefix."groups.`Status` = 'true' AND
            ".prefix."groups.ID = $group AND
            ".prefix."user_group.UserID = $user_id
        ";
		
        $rs = parent::mysql_fetch_one_rows($sql);   
		parent::mysql_close_connection();
        if(empty($rs)){
            return false;
        }else{
            return $rs;
        }
    }
    
    public function model_get_router_permission($seo_key, $app_name)
    {
        $sql = "
            SELECT
            ".prefix."categories_lang.SeoKey,
            ".prefix."categories.ID
            FROM
            ".prefix."categories
            INNER JOIN ".prefix."categories_lang ON ".prefix."categories.ID = ".prefix."categories_lang.CatID
            WHERE
            ".prefix."categories_lang.LangID = ".LANG." AND
            ".prefix."categories_lang.SeoKey = '$seo_key' AND
            ".prefix."categories_lang.`Status` = 'show' AND
            ".prefix."categories.App = '$app_name'

        ";
        
        $rs = parent::mysql_fetch_one_rows($sql);   
		parent::mysql_close_connection();
        if(empty($rs)){
            return false;
        }else{
            return $rs;
        }
    }
}